package cn.facetoken.homer;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.appbar.CollapsingToolbarLayout;

import java.util.ArrayList;
import java.util.List;

public class FaceRegistrationListFragment extends Fragment implements View.OnClickListener {

    public FaceRegistrationListFragment() {
        super();
    }

    private RecyclerView recyclerView;

    private Button registerPhotoButton;

    private FaceSampleDao faceSampleDao;

    @Override
    public void onCreate(@Nullable final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        CollapsingToolbarLayout appBarLayout = requireActivity().findViewById(R.id.toolbar_layout);
        if (null != appBarLayout) {
            appBarLayout.setTitle(getResources().getString(R.string.demonstration_face_registration_title));
        }

        faceSampleDao = MainApplication.getMainDatabase().faceSampleDao();
    }

    @Override
    public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable final ViewGroup container,
                             @Nullable final Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        @NonNull final View rootView = inflater.inflate(R.layout.demonstration_face_registration_list, container, false);
        registerPhotoButton = rootView.findViewById(R.id.register_photo_button);
        registerPhotoButton.setOnClickListener(this);

        recyclerView = rootView.findViewById(R.id.face_registration_list);
        setupRecyclerView(recyclerView);

        return rootView;
    }

    private void setupRecyclerView(@NonNull final RecyclerView recyclerView) {
        new Thread(() -> {
            final List<FaceSample> sampleList = faceSampleDao.loadAll();
            final List<PhotoRegistration<?>> photoList = new ArrayList<>(sampleList.size());
            for (FaceSample sample : sampleList) {
                photoList.add(sample.toPhotoRegistration());
            }
            //final List<PhotoRegistration> photoList = sampleList.stream().map(FaceSample::toPhotoRegistration).collect(Collectors.toList());

            requireActivity().runOnUiThread(() -> recyclerView.setAdapter(new PhotoRegistrationRecyclerViewAdapter(requireActivity(), photoList)));
        }).start();
    }

    @Override
    public void onClick(@NonNull final View view) {
        if (registerPhotoButton == view) {
            Log.d(getClass().getSimpleName(), "onClick(): Register Photo Button was clicked.");
            //getActivity().getSupportFragmentManager().beginTransaction().add();
            requireActivity().getSupportFragmentManager().beginTransaction()
                    .replace(R.id.demonstration_container, new FaceRegistrationFragment(), "FaceRegistration")
                    .commit();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        recyclerView.invalidate();
    }
}
